import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import * as echarts from 'echarts';
import '/public/font/iconfont.css'
import Axios from "axios"
import dongRouter from "./router/dongRouter";
import el from "element-ui/src/locale/lang/el";

Vue.prototype.$axios = Axios;
Vue.prototype.$echarts = echarts;
Vue.config.productionTip = false;
Vue.use(ElementUI);


Axios.defaults.headers.common['Authentication-Token'] = store.state.Authorization;

//设置请求头
Axios.interceptors.request.use(
    config => {
      if(sessionStorage.getItem('Authorization')){
        config.headers.common['Authentication-Token'] = sessionStorage.getItem('Authorization');
      }
      return config;
    },
    error => {
      return Promise.reject(error);
    }
);

//全局守卫
router.beforeEach((to,from,next)=>{
  if(to.name == 'login'){
    next();
  }else{
    let token = sessionStorage.getItem('Authorization');
    if(token == '' || token == null){
      next('/login');
    }else{
        if (store.getters.getRefresh) {
            // 如果用户登录了，页面refresh值为true，则重新添加路由
            store.dispatch('setNoRefresh'); // 重新刷新设置为false
            dongRouter.DynamicAddRouter(); // 添加动态路由的方法
            next({ ...to, replace: true })
        } else {
            next()
        }
    }
  }
});

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app');
